#include "admin.h"

// 用于 for_each 的遍历操作
void print_book(book temp)
{
	cout << "书名:" << temp.get_book_name();
	cout << "\t作者:" << temp.get_author();
	cout << "\t价格:" << temp.get_price();
	cout << (temp.get_is_sta() ? "在馆" : "借出") << endl;
}

admin::admin()
	: book_v(MAX_BOOK_NUM)
{
}

admin::~admin()
{
}

void admin::add_book(const book &new_book)
{
	book_v.push_back(new_book);
}

void admin::delete_book(const string &book_name)
{
	this->book_name = book_name;
	// 查找
	book temp_book;
	temp_book.set_book_name(book_name);
	vector<book>::iterator it = find(this->book_v.begin(), this->book_v.end(), temp_book);

	cout << it->get_author() << endl;

	// 删除
	book_v.erase(it);
}

// 修改书籍信息
void admin::modify_book(const string &book_name, const book &updated_book)
{

	// 查找
	book temp_book;
	temp_book.set_book_name(book_name);
	vector<book>::iterator it = find(this->book_v.begin(), this->book_v.end(), temp_book);
	it->set_book_name(updated_book.get_book_name());
	it->set_author(updated_book.get_author());
	it->set_price(updated_book.get_price());
	it->set_is_sta(updated_book.get_is_sta());
}


book admin::search_book(const string &book_name)
{
	// 查找
	book temp_book;
	temp_book.set_book_name(book_name);
	vector<book>::iterator it = find(this->book_v.begin(), this->book_v.end(), temp_book);

	return *it;
}

// 遍历
void admin::view_all_books() const
{
	for_each(this->book_v.begin(), this->book_v.end(), print_book);
}

bool admin::find_book_name(book book_temp)
{
	return book_temp.get_book_name() == this->book_name;
}
